using System.Web.Mvc;
using ExtendedMvc.Html;

namespace ExtendedMvc.Elements {
	public class Option : OptionBase<Option> {
		public Option(object value, string id = null) : base(value, id) { }
	}

	public abstract class OptionBase<T> : VisibleElement<T> where T : OptionBase<T> {
		private bool _selected;
		private object _value;

		protected object value { get { return _value; } }

		protected OptionBase(object value, string id = null)
			: base(HtmlTag.Option, id) {
			Value(value);
		}

		public virtual T Selected(bool value) {
			_selected = value;
			return (T)this;
		}

		public virtual T Value(object value) {
			_value = value;
			return (T)this;
		}

		protected override TagRenderMode TagRenderMode { get { return TagRenderMode.Normal; } }

		protected override void SetId() {
			Id(string.Format("{0}_{1}", id, _value));
			base.SetId();
		}

		protected override void Prepare() {
			if (_selected) SetAttribute(HtmlAttribute.Selected, HtmlAttribute.Selected);
			base.Prepare();
		}

		protected override void SetContent() {
			if (value == null) return;
			Builder.InnerHtml = value.ToString();
		}
	}
}